// pages/step/step.js
const app = getApp();
import WxValidate from "../../utils/WxValidate.js";
const wh = require("../../utils/wh.js");
var Validate = ""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [{
        name: '未婚',
        value: '未婚',
        checked: 'true'
      },
      {
        name: '已婚',
        value: '已婚',
      },
    ],
    justImg: '../../images/camera.png',
    backImg: '../../images/camera.png',
    justImg2: '../../images/s4.png',
    backImg2: '../../images/s5.png',
    finance_staff: '',
    mobile: '',
    username: '',
    id_card_positive_photo: '',
    id_card_negative_photo: '',
    credit_card_positive_photo: '',
    credit_card_negative_photo: '',
    sid: '',
    loan_id:'',
    auth_modal: false, // 授权窗口
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    download_files: [],
    region: ['福建省'], // 选择地区
    regions: true,
    car_trade_name: '', // 车行名称
    finance_staff: '', // 金融专员名称
    finance_staff_mobile: '', // 金融专员联系方式
    client_manager: '', // 客户经理名称
  },
  /**
   * 改变地区
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value,
      regions: false,
    })
  },
  /**
   * 上传图片
   */
  uploadPic: function(e) {
    var that = this,
      param_name = e.currentTarget.dataset.param
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths,
          api = '/api/madetoorder.loan.index/add_user_material',
          filePath = tempFilePaths[0];
        let data = wh.Wxupload(api, filePath, param_name).then(function(res) {
          switch (param_name) {
            case 'id_card_positive_photo':
              that.setData({
                id_card_positive_photo: res.path,
                justImg: filePath
              });
              break;
            case 'id_card_negative_photo':
              that.setData({
                id_card_negative_photo: res.path,
                backImg: filePath
              });
              break;
            case 'credit_card_positive_photo':
              that.setData({
                credit_card_positive_photo: res.path,
                justImg2: filePath
              });
              break;
            case 'credit_card_negative_photo':
              that.setData({
                credit_card_negative_photo: res.path,
                backImg2: filePath
              });
              break;
          }
        });
      }
    })
  },
  goPerson: function (formData) {
    let postUrl = '/api/madetoorder.loan.index/stage_pre_one_apply',
      that = this,
      loan_id = that.data.loan_id,
      params = {
        ls_id: that.data.sid,
        id_card_positive_photo: that.data.id_card_positive_photo,
        id_card_negative_photo: that.data.id_card_negative_photo,
        province: that.data.region[0],
        city: that.data.region[1],
        district: that.data.region[2],
        car_trade_name: formData.car_trade_name,
        finance_staff: formData.finance_staff,
        finance_staff_mobile: formData.finance_staff_mobile,
        client_manager: formData.client_manager,
        phone: formData.mobile,
        username: formData.username,
        credit_card_negative_photo: that.data.credit_card_negative_photo,
        credit_card_positive_photo: that.data.credit_card_positive_photo
      };
    wh.Post(postUrl, params, function(res) {
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        complete: function() {
          setTimeout(function() {
            wx.switchTab({
              url: '../person/person',
            })
          }, 2000);
        }
      });
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    this.setData({
      sid: options.step_id,
      loan_id: options.loan_id
    });
    // 授权
    let that = this;
    wx.getExtConfig({
      success: function (res) {
        module.exports.globalData = res.extConfig;
        let s_app_id = wx.getStorageSync('whlw_app_id');
        if (s_app_id != module.exports.globalData.app_id) {
          that.setData({
            auth_modal: true
          })
        }
      }
    });
  },
  // 取消授权
  cancel_auth: function () {
    wx.switchTab({
      url: '../index/index'
    });
    this.setData({
      auth_modal: false
    })
  },
  /**
   * 确认授权
   */
  bindGetUserInfo: function (e) {
    let userInfo = e.detail, that = this;
    this.setData({ userInfo: userInfo });
    if (e.detail.userInfo) {
      wx.showToast({
        title: '授权成功',
      })
      setTimeout(function () {
        let auth_rs = app.wlogin(userInfo);
        let employId = wx.getStorageSync('employId');
        if (!employId || employId == '') {
          app.employIdCallback = employId => {
            if (employId != '') {
              that.setData({
                auth_modal: false
              })
            }
          }
        }
      }, 1500)
    } else {
      wx.switchTab({
        url: '../index/index'
      })
    }
  },
  // 表单提交
  formSubmit: function(e) {
    if (this.data.id_card_positive_photo == '') {
      wx.showToast({
        title: '请选择身份证正面照',
        icon: 'none'
      });
      return false
    }
    if (this.data.id_card_negative_photo == '') {
      wx.showToast({
        title: '请选择身份证反面照',
        icon: 'none'
      });
      return false
    }
    if (this.data.credit_card_positive_photo == '') {
      wx.showToast({
        title: '请选择银行卡正面照',
        icon: 'none'
      });
      return false
    }
    if (this.data.credit_card_negative_photo == '') {
      wx.showToast({
        title: '请选择银行卡反面照',
        icon: 'none'
      });
      return false
    }
    // 数据进行校验  
    let rules = {
      mobile: {
        required: true,
        tel: true,
      },
      province_name: {
        required: true
      },
      car_trade_name: {
        required: true
      },
      finance_staff: {
        required: true
      },
      finance_staff_mobile: {
        required: true,
        tel: true,
      },
      client_manager: {
        required: true
      }
    }
    // 验证字段的提示信息，若不传则调用默认的信息  
    let messages = {
      mobile: {
        required: '请输入手机号码',
        tel: '请输入正确的手机号码',
      },
      province_name: {
        required: '请选择地区'
      },
      car_trade_name: {
        required: '请输入车行名称'
      },
      finance_staff: {
        required: '请输入金融专员名称'
      },
      finance_staff_mobile: {
        required: '请输入金融专员联系方式',
        tel: '请输入有效联系方式'
      },
      client_manager: {
        required: '请输入客户经理名称'
      }
    }
    Validate = new WxValidate(rules, messages);
    var that = this;
    var formData = e.detail.value;
    if (!Validate.checkForm(e)) {
      const error = Validate.errorList[0]
      // 提示信息  
      wx.showToast({
        title: error.msg,
        icon: 'none'
      });
      return false
    }
    this.goPerson(formData)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
})